package com.hengtongxing.hejiayun.homepage;


import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.github.chrisbanes.photoview.PhotoView;
import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.BaseActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BigImageActivity extends BaseActivity {
    @BindView(R.id.page)
    TextView page;
    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.relative)
    RelativeLayout mRelativeLayout;
    private List<String> list;
    private PageAdapter pageAdapter;
    private int current;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_image);
        ButterKnife.bind(this);
        initData();
    }

    public void initData() {
        list = (List<String>) getIntent().getSerializableExtra("list");
        current = getIntent().getIntExtra("position", 0);
        page.setText(current + 1 + "/" + list.size());
        pageAdapter = new PageAdapter(list, this);
        vp.setAdapter(pageAdapter);
        vp.setCurrentItem(current);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                current = position;
                page.setText(current + 1 + "/" + list.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mRelativeLayout.setOnClickListener(view -> {
            finish();
//            overridePendingTransition(R.anim.screen_zoom_in, R.anim.screen_zoom_out);
        });
    }

    class PageAdapter extends PagerAdapter {
        List<String> imagesUrl;
        Context context;

        public PageAdapter(List<String> imagesUrl, Context context) {
            this.imagesUrl = imagesUrl;
            this.context = context;
        }

        @Override
        public int getCount() {
            return (imagesUrl == null || imagesUrl.size() == 0) ? 0 : imagesUrl.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            String url = imagesUrl.get(position);

            View view = LayoutInflater.from(context).inflate(R.layout.item_simple_pager, container, false);

            ProgressBar progressBar = view.findViewById(R.id.progressBar);
            PhotoView photoView = view.findViewById(R.id.photoView);
            progressBar.setVisibility(View.VISIBLE);

            Glide.with(context)
                    .asBitmap()
                    .load(url)
                    .listener(new RequestListener<Bitmap>() {
                        @Override
                        public boolean onLoadFailed(GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                            progressBar.setVisibility(View.GONE);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                            progressBar.setVisibility(View.GONE);
                            return false;
                        }
                    })
                    .into(photoView);

            container.addView(view);
            photoView.setOnClickListener(v -> {
                finish();
//                overridePendingTransition(R.anim.screen_zoom_in, R.anim.screen_zoom_out);
            });
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

}
